﻿using System;
using System.Diagnostics;
using System.ServiceModel;

namespace Conducido.WcfMessageLogger.Services.Behaviors.Entities
{
    /// <summary>
    /// Wrapper class for getting a correlationId into a service
    /// Across request/response or in servicecomposition
    /// </summary>
    public class CorrelationContext
    {
        private const string CorrelationIdKey = "Conducido.WcfMessageLogger.Services.Behavior.CorrelationIdKey";
        private static CorrelationContext _current = new CorrelationContext();

        private CorrelationContext()
        {
        }

        /// <summary>
        /// Gets the current 
        /// </summary>
        public static CorrelationContext Current
        {
            get { return _current ?? (_current = new CorrelationContext()); }
        }

        /// <summary>
        /// The correlationId itself
        /// </summary>
        public Guid CorrelationId
        {
            get { return GetCorrelationId(); }
        }

        internal void Initialize(Guid correlationId)
        {
            _current.SetActivityId(correlationId);
        }

        private Guid GetCorrelationId()
        {
            if (OperationContext.Current != null && 
                OperationContext.Current.IncomingMessageProperties.ContainsKey(CorrelationIdKey))
            {
                return (Guid) OperationContext.Current.IncomingMessageProperties[CorrelationIdKey];
            }
            Debug.WriteLine("There is no CorrelationId found in OperationContext");
            return Guid.Empty; // If CorrelationId is not set on OperationContext then return Guid.Empty
        }

        /// <summary>
        /// Sets the Activity id on the <see cref="OperationContext"/>
        /// </summary>
        /// <param name="guid">The CorrelationId</param>
        private void SetActivityId(Guid guid)
        {
            if (OperationContext.Current.IncomingMessageProperties.ContainsKey(CorrelationIdKey))
            {
                OperationContext.Current.IncomingMessageProperties[CorrelationIdKey] = guid;
            }
            else
            {
                OperationContext.Current.IncomingMessageProperties.Add(CorrelationIdKey, guid);
            }
        }

        private void CleanActivityId()
        {
            try
            {
                if (OperationContext.Current.IncomingMessageProperties.ContainsKey(CorrelationIdKey))
                {
                    OperationContext.Current.IncomingMessageProperties.Remove(CorrelationIdKey);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// Cleans the object and removes the CorrelationId from
        /// the <see cref="OperationContext"/>
        /// </summary>
        public void Clean()
        {
            CleanActivityId();
            _current = null;
        }
    }
}